package com.sgb.kjwl.view.ui.adapter.manage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.swgk.core.util.LogUtil;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemManageListBinding;
import com.sgb.kjwl.interfaces.RecyclerClick;
import com.sgb.kjwl.model.entity.manage.TheEnterpriseOfEntityForManageList;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

/***
 * 企业经营-推荐企业-最新入驻
 */
public class ManageEnterprisesAdapter extends BaseRecycleAdapter<TheEnterpriseOfEntityForManageList> {
    private static final String TAG = "RecommendedEnterprisesAdapter";
    private RecyclerClick<TheEnterpriseOfEntityForManageList> mRecyclerClick;

    public void setmRecyclerClick(RecyclerClick<TheEnterpriseOfEntityForManageList> mRecyclerClick) {
        this.mRecyclerClick = mRecyclerClick;
    }

    public ManageEnterprisesAdapter(Context context, List<TheEnterpriseOfEntityForManageList> datas) {
        super(context, R.layout.item_manage_list, datas);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void convert(ItemViewHolder holder, final TheEnterpriseOfEntityForManageList entity) {
        holder.setBinding(BR.viewModel, entity);
        ItemManageListBinding binding = (ItemManageListBinding) holder.getmBinding();
        if (entity == null) {
            LogUtil.d(TAG, "RecommendedEnterprisesAdapter entity is null");
            return;
        }

        if (holder.getIndex_position() == 0) {
            binding.tvPositionLine.setVisibility(View.GONE);
        } else {
            binding.tvPositionLine.setVisibility(View.VISIBLE);
        }
//        int width = WidthUtils.width(binding.llType.getMeasuredWidth());
//        binding.tvIndustryType.setMaxWidth(width);
//        binding.tvManageType.setMaxWidth(width);
//        binding.tvRegisteredCapital.setMaxWidth(width);
//        binding.tvEnterpriseType.setMaxWidth(width);
        setVisible(new TextView[]{binding.tvLine1, binding.tvLine2, binding.tvLine3}, new String[]{(TextUtils.isEmpty(entity.getIndustryCategoryTwoName()) ? entity.getIndustryCategoryOneName() : entity.getIndustryCategoryTwoName())
                , entity.getServiceType(), entity.getRegisteredCapital(), entity.getCompanyType()}, new TextView[]{binding.tvIndustryType, binding.tvManageType, binding.tvRegisteredCapital, binding.tvEnterpriseType});

        binding.tvFollow.setBackgroundResource(entity.getFollow() ? R.drawable.bg_gz_selected : R.drawable.bg_gz_normal);
        binding.tvFollow.setText(entity.getFollow() ? "已关注" : "关注");
        binding.tvFollow.setTextColor(entity.getFollow() ? context.getResources().getColor(R.color.color_969696) : context.getResources().getColor(R.color.color_main));

        TextView tv = holder.getItem().findViewById(R.id.tv_follow);
        tv.setOnClickListener(v -> {
            if (mRecyclerClick != null) {
                mRecyclerClick.followClick(entity);
            }
        });

        if (binding.tvAddress.getVisibility() == View.GONE && binding.tvDistance.getVisibility() == View.GONE) {
            binding.rlAddressManageList.setVisibility(View.GONE);
        } else {
            binding.rlAddressManageList.setVisibility(View.VISIBLE);
        }
    }

    private void setVisible(TextView[] line, String[] content, TextView[] textViews) {
        line[0].setVisibility(View.GONE);
        line[1].setVisibility(View.GONE);
        line[2].setVisibility(View.GONE);
        int num = 0;
        for (String s : content) {
            if (!TextUtils.isEmpty(s)) {
                textViews[num].setText(s);
                num++;
            }
        }

        switch (num) {
            case 0:
                line[0].setVisibility(View.GONE);
                line[1].setVisibility(View.GONE);
                line[2].setVisibility(View.GONE);
                textViews[0].setVisibility(View.GONE);
                textViews[1].setVisibility(View.GONE);
                textViews[2].setVisibility(View.GONE);
                textViews[3].setVisibility(View.GONE);
                break;
            case 1:
                line[0].setVisibility(View.GONE);
                line[1].setVisibility(View.GONE);
                line[2].setVisibility(View.GONE);
                textViews[0].setVisibility(View.VISIBLE);
                textViews[1].setVisibility(View.GONE);
                textViews[2].setVisibility(View.GONE);
                textViews[3].setVisibility(View.GONE);
                break;
            case 2:
                line[0].setVisibility(View.VISIBLE);
                line[1].setVisibility(View.GONE);
                line[2].setVisibility(View.GONE);
                textViews[0].setVisibility(View.VISIBLE);
                textViews[1].setVisibility(View.VISIBLE);
                textViews[2].setVisibility(View.GONE);
                textViews[3].setVisibility(View.GONE);
                break;
            case 3:
                line[0].setVisibility(View.VISIBLE);
                line[1].setVisibility(View.VISIBLE);
                line[2].setVisibility(View.GONE);
                textViews[0].setVisibility(View.VISIBLE);
                textViews[1].setVisibility(View.VISIBLE);
                textViews[2].setVisibility(View.VISIBLE);
                textViews[3].setVisibility(View.GONE);
                break;
            case 4:
                line[0].setVisibility(View.VISIBLE);
                line[1].setVisibility(View.VISIBLE);
                line[2].setVisibility(View.VISIBLE);
                textViews[0].setVisibility(View.VISIBLE);
                textViews[1].setVisibility(View.VISIBLE);
                textViews[2].setVisibility(View.VISIBLE);
                textViews[3].setVisibility(View.VISIBLE);
                break;
            default:
                line[0].setVisibility(View.GONE);
                line[1].setVisibility(View.GONE);
                line[2].setVisibility(View.GONE);
                textViews[0].setVisibility(View.VISIBLE);
                textViews[1].setVisibility(View.VISIBLE);
                textViews[2].setVisibility(View.VISIBLE);
                textViews[3].setVisibility(View.VISIBLE);
                break;
        }
    }

}
